<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.js"></script>
</head>
<body>
    <div id="app">
        <!-- 用v-if控制组件显示,影响生命周期 -->
        <!-- <counter v-if="isShow"></counter> -->
        <!-- 用v-show控制组件,不会影响生命周期 -->
        <!-- <counter v-show="isShow"></counter> -->
        <!-- 动态组件 -->
        <!-- 用动态组件控制组件显示,影响生命周期 -->
        <keep-alive>
            <component :is="compName"></component>
        </keep-alive>
    </div>
</body>
<script>
Vue.component('counter', {
    data(){
        return {
            count: 0
        }
    },
    template: `
        <div>count={{count}},<button @click="count++">+1</button></div>
    `
})
let vm = new Vue({
    el: '#app',
    data(){
        return {
            isShow: true,
            compName: 'counter'
        }
    }
})
</script>
</html>